package com.zxl.permission.controller;


import com.alibaba.fastjson.JSONObject;
import com.zxl.permission.entity.Group;
import com.zxl.permission.service.GroupService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

// TODO: 今天继续从这里实现
// TODO: 用户所属用户组的添加、删除功能

@RestController
@RequestMapping("/group")
public class GroupController {
    @Autowired
    private GroupService groupService;

    @RequestMapping(value = "/list", method = RequestMethod.POST)
    @ResponseBody
    public String list() {
        JSONObject res = groupService.list();
        return res.toJSONString();
    }

    @RequestMapping(value = "/detail", method = RequestMethod.POST)
    @ResponseBody
    public String detail(@RequestBody Map<String, Object> body) {
        String groupname = (String) body.get("groupname");
        JSONObject res = groupService.detail(groupname);
        return res.toJSONString();
    }

    @RequestMapping(value = "/add", method = RequestMethod.POST)
    @ResponseBody
    public String add(@RequestBody Group group) {
        JSONObject res = groupService.add(group);
        return res.toJSONString();
    }

    @RequestMapping(value = "/remove", method = RequestMethod.POST)
    @ResponseBody
    public String remove(@RequestBody Map<String, Object> body) {
        String groupname = (String) body.get("groupname");
        JSONObject res = groupService.remove(groupname);
        return res.toJSONString();
    }

    @RequestMapping(value = "/user_group_list", method = RequestMethod.POST)
    @ResponseBody
    public String userGroupList() {
        JSONObject res = groupService.userGroupList();
        return res.toJSONString();
    }

    @RequestMapping(value = "/add_user", method = RequestMethod.POST)
    @ResponseBody
    public String addUser(@RequestBody Map<String, Object> body) {
        String groupname = (String) body.get("groupname");
        String username = (String) body.get("username");
        JSONObject res = groupService.addUser(groupname, username);
        return res.toJSONString();
    }


}
